package com.polaris.live.ui.message.fragment.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import com.polaris.live.R
import com.polaris.live.common.ext.dp
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.FragmentDialogMessageSessionBinding
import com.polaris.live.im.sfs.COMMAND_SYSTEM_MESSAGE
import com.polaris.live.ui.message.fragment.MessageSessionFragment
import com.polaris.live.utils.fragment.attachFragment

/**
 * 直播的消息会话列表，这个fragment默认是复用的[isReusable]
 *
 * @author Created by lucas on 2023/10/28 10:52
 */
class MessageSessionDialogFragment(
    private val anchorId: Long? = null,
    override val isReusable: Boolean = true
) : BottomDialogFragment() {

    private lateinit var mBinding: FragmentDialogMessageSessionBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        return FragmentDialogMessageSessionBinding.inflate(inflater, container, false).let {
            mBinding = it

            it.root
        }
    }

    private val mFragment by lazy {
        val needClickAvatar = arguments?.getBoolean(LiveConst.Params.NEED_CLICK_AVATAR, true) ?: true
        childFragmentManager.attachFragment(R.id.fl_fragment) {
            MessageSessionFragment().apply {
                arguments = bundleOf(
                    "sessionCommand" to COMMAND_SYSTEM_MESSAGE,
                    "nestedPage" to true,
                    LiveConst.Params.NEED_CLICK_AVATAR to needClickAvatar,
                    "anchorId" to anchorId
                )
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        NavigationBarUtils.setNavigationHeightIfTransparent(mBinding.vBottomLine)

        //初始化
        mFragment

        //背景
        ViewUtils.setCardViewBackground(
            mBinding.flBg,
            R.color.white,
            topLeftCorner = 12.dp,
            topRightCorner = 12.dp
        )
    }

    override fun getTopMargin(): Int {
        return super.getTopMargin() - 20.dp
    }

    companion object {

        val FRAGMENT_TAG = "MessageDialogSessionFragment"
    }
}